package com.sais.common.entity;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 通用返回数据类
 * </p>
 *
 * @author zwp
 * @since 2019-09-24
 */
public class Result extends HashMap {
    private static final long serialVersionUID = 1L;

    public static final String CODE = "code";
    public static final String MSG = "msg";
    public static final String DATA = "data";

    public static final Object[] CODE_200 = {200, "请求成功"};

    public static final Object[] CODE_400 = {400, "请求参数错误"};
    public static final Object[] CODE_401 = {401, "登录失效或者用户未登录"};
    public static final Object[] CODE_402 = {402, "验证码错误"};
    public static final Object[] CODE_403 = {403, "系统繁忙"};
    public static final Object[] CODE_405 = {405, "权限不足"};

    public static final Object[] CODE_500 = {500, "服务器内部错误，联系管理员"};
    public static final Object[] CODE_501 = {501, "服务器内部错误，数据库异常"};

    private Map<String, Object> data = new HashMap<String, Object>();

    public Result() {
        put(CODE, Result.CODE_200[0]);
        put(MSG, Result.CODE_200[1]);
        put(DATA, data);
    }

    public Result(Object[] strings) {
        put(CODE, strings[0]);
        put(MSG, strings[1]);
        put(DATA, data);
    }

    public Result(Object[] strings, String msg) {
        put(CODE, strings[0]);
        put(MSG, msg);
        put(DATA, data);
    }

    public Result(Integer code, String msg) {
        put(CODE, code);
        put(MSG, msg);
        put(DATA, data);
    }

    public static Result response() {
        return new Result();
    }

    public static Result response(Object[] strings) {
        return new Result(strings);
    }

    public static Result response(Object[] strings, String msg) {
        return new Result(strings, msg);
    }

    public static Result response(Integer code, String msg) {
        return new Result(code, msg);
    }

    public Result data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public Result data(Map<String, Object> map) {
        this.data.putAll(map);
        return this;
    }
}
